﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameDataLibrary;

namespace UtopianGame
{
    class RandomEvent
    {
        public RandomEventData xmlData;

        public RandomEvent(RandomEventData newXMLData)
        {
            xmlData = newXMLData;
        }

        //runEvent is unusual, in that it's a bool function that does more than just return true or false.
        //It returns false if the event cannot be run.
        //If the event can be run, it runs it and returns true.
        public bool runEvent()
        {
            if (areEventPrereqsMet())
            {
                StatusEffect statusEffectToAdd = null;
                //find the status effect to add to the Nation.
                for (int i = 0; i < GlobalDataHolder.statusEffectArray.Length; i++)
                {
                    if (GlobalDataHolder.statusEffectArray[i].xmlData.name == xmlData.statusEffectName)
                    {
                        statusEffectToAdd = GlobalDataHolder.statusEffectArray[i];
                    }
                }

                Nation.statusEffectList.Add(statusEffectToAdd);

                //Add the event's name and description.
                GlobalDataHolder.randomEventName = xmlData.name;
                GlobalDataHolder.randomEventDescription = xmlData.description;

                return true;   //The event is run.
            }
            else
            {
                return false;  //The event is not run. Another one will be picked.
            }
        }

        public bool areEventPrereqsMet()
        {
            if (xmlData.prereqFunctionName == "isHealthAbove75")
            {
                for (int i = 0; i < Nation.regionList.Count; i++)
                {
                    if (Nation.regionList[i].healthIndex > 75)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            if (xmlData.prereqFunctionName == "isHealthBelow50")
            {
                for (int i = 0; i < Nation.regionList.Count; i++)
                {
                    if (Nation.regionList[i].healthIndex < 50)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            if (xmlData.prereqFunctionName == "isHealthBelow25")
            {
                for (int i = 0; i < Nation.regionList.Count; i++)
                {
                    if (Nation.regionList[i].healthIndex < 25)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }

            //If the prereqFunctionName doesn't match a defined one, just return true.
            return true;
        }
    }
}
